using System;
using System.Collections;
using System.Collections.Generic;
using System.Timers;
using UnityEngine;

public class SkillManger : Singleton<SkillManger>
{
    private Vector2 direction;
    [Header("泡泡技能")] public GameObject bubblePerfab;
    [HideInInspector]public bool haveBubble;
    [SerializeField] private float bubbleFlyTime;
    private float _bubbleFlyTime;
    [SerializeField] private float bubbleCoolDown;
    private float _bubbleCoolDown;
    private GameObject bubble;
    [Header("泡泡生成位置偏移量")]
    [SerializeField] private Vector2 bubbleOffset;

    [HideInInspector]public bool isDash;
    [HideInInspector]public bool canDash;
    [Header("冲刺相关")]
    [SerializeField] private float dashCoolDownTime;
    [SerializeField]private float dashTime;
    [SerializeField] private float dashForce;

    public BoxCollider2D destroyCollArea;

    [Header("回溯相关")] 
    [SerializeField] private float backTime;
    private bool _startBackTime;
    private GameObject _backPlayer;
    [SerializeField] private GameObject playerShadow;
    private float _shouldWaitTime;
    
    private Rigidbody2D _rb;
    [HideInInspector] public bool isReleaseSkill;

    private float _releaseSkillTime;
    

    protected override void Awake()
    {
        base.Awake();
        _rb = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
    }

    private void Start()
    {
        canDash = true;
    }

    private void Update()
    {
        direction = _rb.transform.localScale.normalized;
        if(!isDash)
            DashTimeCounter();
        if(_startBackTime)
            BackTimeCounter();
        if (haveBubble)
        {
            _bubbleCoolDown -= Time.deltaTime;
            _bubbleFlyTime -= Time.deltaTime;
        }

        if (_bubbleFlyTime <= 0)
        {
            Destroy(bubble);
            haveBubble = false;
            isReleaseSkill = false;
        }
    }

    public void ReleaseSkill(Personas curPersona)
    {
        switch (curPersona)
        {
            case Personas.Blue:
                if (!haveBubble)
                {
                    bubble = Instantiate(bubblePerfab, new Vector2(_rb.position.x + bubbleOffset.x * direction.x, _rb.position.y + bubbleOffset.y), Quaternion.identity);
                    haveBubble = true;
                    _bubbleCoolDown = bubbleCoolDown;
                    _bubbleFlyTime = bubbleFlyTime;
                }
                else if (haveBubble && _bubbleCoolDown <= 0)
                {
                    Destroy(bubble);
                    bubble = Instantiate(bubblePerfab, new Vector2(_rb.position.x + bubbleOffset.x * direction.x, _rb.position.y + bubbleOffset.y), Quaternion.identity);
                    _bubbleCoolDown = bubbleCoolDown;
                    _bubbleFlyTime = bubbleFlyTime;
                }
                

                break;
            case Personas.Red :
                if (canDash)
                {
                    StartCoroutine(Dash());
                }
                break;
            case Personas.Gold :
                GoBackTimePosition();
                break;
            case Personas.Green :
                break;
        }
        
    }
    
    /// <summary>
    /// 红色人格冲刺
    /// </summary>
    /// <returns></returns>
    private IEnumerator Dash()
    {
        isDash = true;
        canDash = false;
        float dashingGravity = _rb.gravityScale;
        _rb.gravityScale = 0f;
        
        _rb.velocity = new Vector2(_rb.gameObject.transform.localScale.x * dashForce, 0f);
        destroyCollArea.gameObject.SetActive(true);
        yield return new WaitForSeconds(dashTime);
        destroyCollArea.gameObject.SetActive(false);
        _rb.gravityScale = dashingGravity;
        isDash = false;
        _releaseSkillTime = dashCoolDownTime;
        // canDash = true;
    }

    /// <summary>
    /// 冲刺时间内所碰到的物体是不是能撞坏
    /// </summary>
    private void DashTimeCounter()
    {
        _releaseSkillTime -= Time.deltaTime;
        if (_releaseSkillTime <= 0)
            canDash = true;
    }


    /// <summary>
    /// 时间回溯
    /// </summary>
    private void GoBackTimePosition()
    {
        if (!_startBackTime)
        {
            _startBackTime = true;
            _backPlayer = Instantiate(playerShadow, transform.position, Quaternion.identity);
            _shouldWaitTime = backTime;
        }
        else
        {
            _rb.gameObject.transform.position = _backPlayer.transform.position;
            _rb.gameObject.transform.localScale = _backPlayer.transform.localScale;
            Destroy(_backPlayer);
            _startBackTime = false;
        }
    }

    private void BackTimeCounter()
    {
        _shouldWaitTime -= Time.deltaTime;
        if (_shouldWaitTime <= 0)
        {
            _startBackTime = false;
            Destroy(_backPlayer);
        }

    }
}
